﻿using System;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject;
using DataObject.Image;
using DataObject.Product;

namespace KMT.Controls.Product
{
    public partial class ProductDetail : System.Web.UI.UserControl
    {
        public int ProductId { get; set; }

        protected int CategoryId;        
        protected string Type;
        protected string Code;
        protected string Name;
        protected string Description;
        protected string Content;
        protected string ImagePath;
        protected string Images;
        protected string UnitPrice;
        protected string SpecialPrice;

        protected void Page_Load(object sender, EventArgs e)
        {            
            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "kmt",
                "<script>listComment(" + ProductId + ", " + (int)CategoryType.Product + ", " + SettingValues.PageIndex + ");</script>");

            Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "kmt",
                "<script>listRelatedProduct(" + CategoryId + ", " + ProductId + ", " + (int)CategoryType.Product + ", " + SettingValues.PageIndex + ");</script>");

            if (!IsPostBack)
                GetProductDetail();
        }

        private void GetProductDetail()
        {        
            try
            {
                var o = ProductController.Product_GetById(ProductId);
                if (o != null)
                {
                    Code         = o.Code;
                    Name         = o.Name;
                    Description  = o.Description;
                    Content      = o.Content;
                    ImagePath    = string.Format("{0}{1}", Constants.ThumbImage, o.ImagePath);
                    Images       = o.Images;
                    UnitPrice    = Utils.FormatPrice(ConvertUtility.ToDecimal(o.UnitPrice));
                    SpecialPrice = Utils.FormatPrice(ConvertUtility.ToDecimal(o.SpecialPrice));
                    CategoryId   = o.CategoryId;
                    Type         = Constants.GetProductType(o.Type);

                    var imgHref = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size={3}",
                                                Constants.ThumbImage, ProductId, (int)CategoryType.Product, Constants.ThumbImageSizeLargeDetail);

                    ltImage.Text = string.Format("<img class=\"zoom-image\" src=\"{0}\" alt==\"{1}\" title=\"{1}\" />", imgHref, Name);
                    //ltZoomImage.Text = string.Format("<a id=\"zoom\" class=\"lightbox\" href=\"{0}\"></a>", imgHref);

                    var lstImage = ImageController.Image_GetByProductId(ProductId);
                    if (lstImage != null && lstImage.Count > 0)
                    {                        
                        rptThumbImage.DataSource = lstImage;
                        rptThumbImage.DataBind();
                    }

                    Page.Title = string.Format(".: {0} - {1} {2} :.", o.Code, o.Name, Constants.SITE_TITLE);
                }
            }
            catch { }        
        }  

        protected void rptThumbImage_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
            {
                var ltImage   = (Literal)e.Item.FindControl("ltImage");                    
                var imageId   = ((HiddenField)e.Item.FindControl("hfImageId")).Value;
                var imagePath = ((HiddenField)e.Item.FindControl("hfImagePath")).Value;                

                //var imgLarge = string.Format("{0}{1}", Constants.ThumbImage, imagePath);
                //var imgSmall = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size={3}", 
                //                                Constants.ThumbImage, imageId, (int)CategoryType.Image, 68);
                //width=\"68\" height=\"68\" 

                var imgSmall = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size={3}",
                                                Constants.ThumbImage, imageId, (int)CategoryType.Image, Constants.ThumbImageSizeSmallDetail);

                var imgLarge = string.Format("{0}/Thumb.ashx?Id={1}&tId={2}&Size={3}",
                                                Constants.ThumbImage, imageId, (int)CategoryType.Image, Constants.ThumbImageSizeLargeDetail);

                ltImage.Text = string.Format("<a class=\"zoom-thumbnail\" href=\"{0}\" data-easyzoom-source=\"{1}\" title=\"\">" +
                                             "<img src=\"{0}\" alt=\"\" algin=\"center\" /></a>", imgSmall, imgLarge);
            }    
        }        
    }
}